using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Week1.player
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Camera : Microsoft.Xna.Framework.DrawableGameComponent, helper.IDebugable
    {

        #region variables
        public Matrix Projection;
        public Vector3 Position;
        public Matrix View;
        public Vector3 LookAt;
        public Vector3 oVectorUp;
        protected Vector3 Direction;
        protected int ilastZoomValue = 0;
        int iMouseSpeed = 8;
        #endregion

        public Camera(Game game)
            : base(game)
        {
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            initCamera();
            oVectorUp = new Vector3(0, 1, 0);

    
            base.Initialize();
        }

        /// <summary>
        /// 
        /// </summary>
        private void initCamera()
        {
            Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)this.Game.Window.ClientBounds.Width /
                (float)this.Game.Window.ClientBounds.Height, 1, 10000);
            Position = new Vector3(-80, -80, 50);
            LookAt = new Vector3(-80, -50, 0);
            View = Matrix.CreateLookAt(Position, LookAt, Vector3.Up);
            
        }

        protected override void LoadContent()
        {
            helper.DebugConsole oConsole = helper.DebugConsole.Instance;

            oConsole.registerDebug(this);

        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            iMouseSpeed = 1;

            MouseState mouseState = Mouse.GetState();

            if (Keyboard.GetState().IsKeyDown(Keys.A) || mouseState.X < 10)
            {
                Position.X-=iMouseSpeed;
                LookAt.X -= iMouseSpeed;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.D) || mouseState.X > 1010)
            {
                Position.X += iMouseSpeed;
                LookAt.X += iMouseSpeed;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.W) || mouseState.Y < 10)
            {
                Position.Y += iMouseSpeed;
                LookAt.Y += iMouseSpeed;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.S) || mouseState.Y > 550)
            {
                Position.Y -= iMouseSpeed;
                LookAt.Y -= iMouseSpeed;
            }

            int delta = (ilastZoomValue - Mouse.GetState().ScrollWheelValue);
            ilastZoomValue = Mouse.GetState().ScrollWheelValue;
            Position.Z += (delta * Position.Z) / 2000;

            if (Position.Y < -600)
            {
                Position.Y = -600;
                LookAt.Y = -600;
            }
            else if (Position.Y > 600)
            {
                LookAt.Y = 600;
                Position.Y = 600;
            }
            Matrix.CreateLookAt(ref Position, ref LookAt, ref oVectorUp, out View);


            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }

        #region Debug
        helper.DebugMsg oMsg = new helper.DebugMsg();
        Vector3 debuPos = new Vector3(0, 0, 0);
        helper.DebugMsg helper.IDebugable.getDebugMsg(bool showNext)
        {
            oMsg.message = "mouse = " + Mouse.GetState();
            oMsg.message += "\nposition=" + Position + "\nlookat=" + LookAt + "\nprojection=" + Projection;
            oMsg.message += "\nspeed=" + iMouseSpeed;
            oMsg.scale = 50;
            oMsg.position = debuPos;
            return oMsg;
        }
        #endregion

    }
}
